package com.yj.springbootmybatis.rest;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.yj.springbootmybatis.enums.CategoryType;
import com.yj.springbootmybatis.enums.Show;
import com.yj.springbootmybatis.pojo.Carousel;
import com.yj.springbootmybatis.pojo.Category;
import com.yj.springbootmybatis.pojo.vo.CategoryVo;
import com.yj.springbootmybatis.pojo.vo.NewItemsVo;
import com.yj.springbootmybatis.service.CarouselService;
import com.yj.springbootmybatis.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("index")
@RestController
@Api(value = "首页",tags = "首页轮播图显示")
public class CarouselController {

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ObjectMapper objectMapper;


    /**
     * 1.后台运营系统，一旦广告（轮播图）发送更改，就可以删除缓存，然后重置
     * 2.定时重置，比如每天凌晨三点重置，但是和其他任务要错开
     * 3.每个轮播图都有可能是一个广告，每一个广告都会有一个过期时间，过期了，再重置
     * @return
     * @throws Exception
     */
    @GetMapping("carousel")
    @ApiOperation(value = "获取首页轮播图")
    public List carousel() throws Exception {

        //防止缓存击穿
        String carousel = redisTemplate.opsForValue().get("carousel");
        List<Carousel> list = new ArrayList<>();
        if (StringUtils.isBlank(carousel)) {
            list = carouselService.queryAll(Show.SHOW.type);

            if (null == list || list.size() < 0 ){
                redisTemplate.opsForValue().set("carousel","[]");

            }

            redisTemplate.opsForValue().set("carousel",objectMapper.writeValueAsString(list));
        }else {
            list = objectMapper.readValue(carousel, List.class);
        }
        return list;
    }

    @GetMapping("category")
    @ApiOperation(value = "获取一级分类")
    public List category(){
        List<Category> list = categoryService.queryOneLevel(CategoryType.ONELEVEL.type);
        return list;
    }

    @GetMapping("category/{rootCatId}")
    @ApiOperation(value = "获取一级分类下的子分类",notes = "获取一级分类下的子分类",httpMethod = "GET")
    public List category(@PathVariable @ApiParam(name = "rootCatId",value = "一级分类id",required = true) Integer rootCatId){
        List<CategoryVo> list = categoryService.getSubCatList(rootCatId);
        return list;
    }

    @GetMapping("getSixItems/{rootCatId}")
    @ApiOperation(value = "获取一级分类下的最新六个商品",notes = "获取一级分类下的最新六个商品",httpMethod = "GET")
    public List getSixItems(@PathVariable @ApiParam(name = "rootCatId",value = "一级分类id",required = true) Integer rootCatId){
        if (rootCatId == null){
            return new ArrayList();
        }
        List<NewItemsVo> list = categoryService.getSixNewItems(rootCatId);
        return list;
    }

}
